<?php
/**
 * @资讯类
 * 1、资讯类别
 * 2、资讯列表
 * 3、资讯详情
 */
namespace app\news\controller;
use app\common\controller\HomeCommon;
use think\Cache;
use think\Config;

class Index extends HomeCommon
{
	public function __construct(){
		
		parent::__construct();
		
	
	}

    /**
     * 获得资讯的类别
    */
    public function get_news_type(){
    	
    	$where=['status'=>1];
    	
    	$field="type_name,identification";
    	
    	$model= model('Commondata');;
    	$listobj=$model->get_list('news_type',$field,$where);
    	
    	return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);
    }
   /**
     * 获得资讯列表
   */
    public function get_news(){
    	
    	$where="  a.status=1  ";
    	$identification=request()->param('identification','','trim');
    	
    	
    	
    	$obj=db('news a')
            ->join('news_type d','a.type=d.id','left')
            ->field('a.news_name,a.identification,a.desc,a.thumb,a.froms,a.create_time,a.url,d.type_name');
    	
        /****类型**/
    	if(strlen($identification)){
            $obj->where(['a.status'=>1,'a.type'=>$identification]);
    	}else{
            $obj->where('a.type','neq',0);
        }
    /***关键字*/
    	$keywords=request()->param('keywords','','trim');
    	if(!empty($keywords)){
    		$where.=" and a.news_name like '%$keywords%' ";
    	}
   /***行业*/
    	$industry_id=request()->param('industry_id',0,'intval');
    	if(!empty($industry_id)){
    		$obj=$obj->join('news_industry b','a.id=b.news_id');
    		$where.=" and b.status=1 and b.industry_id=$industry_id";
    	}
  /***产业链*/
    	$chain_id=request()->param('chain_id',0,'intval');
    	if(!empty($chain_id)){
    		$obj=$obj->join('news_chain c','a.id=c.news_id');
    		$where.=" and c.status=1 and c.chain_id=$chain_id";
    	}
    	/***城市*/
    	$city_id=request()->param('city_id',0,'intval');
    	if(!empty($city_id)){
    		$where.=" and (a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id ) ";
    	}




     // 设置分页数据
        $page=request()->param('page',0,'intval');
        $limit=request()->param('limit',15,'intval');
        Config::set('paginate.page',bcadd($page,1));
    	$listobj=$obj->order('create_time','desc')->where($where)->paginate($limit);
    	
    	
    	return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);
    	
   
    }

    /**
     * @return \think\response\Json
     */
    public function get_news_info(){
    	
    	
    	$where['status']=['=',1];
    	
    	
    	$identification=request()->param('identification','','trim');
    	
    	
    	if(!empty($identification)){
    		$where['identification']=['=', $identification];
    		
    		
    		$cache=new Cache();
    		$result=$cache->get('news_'.$identification);
    		
    		
    		if($result){
    			$info=unserialize($result);
    		}else{
    			$field='news_name,identification,desc,thumb,content,froms,create_time';
    			$model= model('Commondata');
    			
    			$info=$model->get_detail('news',$field,$where);
    			$cache->set('news_'.$identification, serialize($info),3600);
    		}
    		
    		
    		return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$info]);
    		
    		
    	}else{
    		return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
    	}
    	
    	
    	
    	
    	
    	
    }
}
